Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update autocontrast #2317

Merged
merged 3 commits into from
Jan 29, 2025
Merged

Update autocontrast #2317

merged 3 commits into from
Jan 29, 2025

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Jan 29, 2025

Summary by Sourcery

Improve the auto_contrast function by adding parameters for cutoff, ignore value, and method selection. Add tests for the new functionality and helper functions.

New Features:

  • Add cutoff parameter to control the percentage of pixels to cut off from the histogram edges.

Tests:

  • Add comprehensive tests for the auto_contrast function, including edge cases and numerical stability.

Copy link
Contributor

sourcery-ai bot commented Jan 29, 2025

Reviewer's Guide by Sourcery

The pull request modifies the auto contrast function to allow for a cutoff and ignore parameters, and adds a method parameter to select between 'cdf' and 'pil' methods. It also adds new tests for the auto contrast function.

Class diagram for updated AutoContrast transform

classDiagram
    class AutoContrast {
        +float cutoff
        +int|None ignore
        +str method
        +float p
        +__init__(cutoff: float, ignore: int|None, method: str, p: float)
        +apply(img: ndarray) -> ndarray
        +apply_to_images(images: ndarray) -> ndarray
        +apply_to_volume(volume: ndarray) -> ndarray
        +apply_to_volumes(volumes: ndarray) -> ndarray
        +get_transform_init_args_names() -> tuple
    }
    note for AutoContrast "New parameters added:
- cutoff: % of pixels to exclude
- ignore: value to preserve
- method: 'cdf' or 'pil'"
Loading

Flow diagram for enhanced auto_contrast function

flowchart TD
    A[Start auto_contrast] --> B{Check image channels}
    B -->|Multi-channel| C[Pre-compute histograms]
    B -->|Single channel| D[Compute histogram]
    C --> E[Process each channel]
    D --> E
    E --> F{Check ignore value}
    F -->|Has ignore value| G[Skip ignored channel]
    F -->|No ignore value| H[Get histogram bounds]
    H --> I{Check cutoff}
    I -->|Has cutoff| J[Calculate bounds with cutoff]
    I -->|No cutoff| K[Use min/max non-zero values]
    J --> L[Create contrast LUT]
    K --> L
    L --> M{Select method}
    M -->|CDF method| N[Apply CDF-based scaling]
    M -->|PIL method| O[Apply linear scaling]
    N --> P[Apply LUT to channel]
    O --> P
    P --> Q[Return enhanced image]
Loading

File-Level Changes

Change Details Files
Modified the auto contrast function to allow for a cutoff and ignore parameters, and adds a method parameter to select between 'cdf' and 'pil' methods.
  • Added cutoff, ignore, and method parameters to the auto_contrast function.
  • Implemented the 'pil' method for auto contrast.
  • Implemented the get_histogram_bounds function to calculate the min and max intensity values based on the cutoff.
  • Implemented the create_contrast_lut function to create the lookup table for contrast adjustment.
  • Modified the auto_contrast function to use the new get_histogram_bounds and create_contrast_lut functions.
  • Added support for ignoring a specific channel when calculating the histogram.
albumentations/augmentations/functional.py
Added new tests for the auto contrast function.
  • Added tests for the create_contrast_lut function.
  • Added tests for the get_histogram_bounds function.
  • Added tests for the auto_contrast function with different parameters.
tests/functional/test_functional.py
Modified the AutoContrast transform to include the new parameters.
  • Added cutoff, ignore, and method parameters to the AutoContrast transform.
  • Modified the apply method to use the new parameters.
  • Modified the get_transform_init_args_names method to include the new parameters.
albumentations/augmentations/transforms.py
Added new parameters to the AutoContrast transform in the test suite.
  • Added cutoff, ignore, and method parameters to the AutoContrast transform in the test suite.
tests/aug_definitions.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ternaus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

tests/functional/test_functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
albumentations/augmentations/functional.py Show resolved Hide resolved
albumentations/augmentations/functional.py Show resolved Hide resolved
tests/functional/test_functional.py Show resolved Hide resolved
@ternaus ternaus merged commit fcda65b into main Jan 29, 2025
14 checks passed
@ternaus ternaus deleted the update_autocontrast branch January 29, 2025 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant